package client;

import java.io.FileNotFoundException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.ArrayList;

import com.itextpdf.text.DocumentException;

import sosr.bean.Medoc;
import sosr.bean.Ordonnance;
import sosr.bean.Patient;
import sosr.pdf.CreatePDF;
import sosr.serveur.IServeurRMI;

public class ClientRMI {
	
	static int port = 53000;
	
public static void main (String[] args) {
		
	try {
		
		Registry reg = LocateRegistry.getRegistry(port);
		reg.lookup("serveurRMI");
		System.out.println("Connexion client \t\t\t\t\t\t\t\t\t\t[OK]");
		IServeurRMI serv = (IServeurRMI) reg.lookup("serveurRMI");
		
		serv.open();
				
		// Test identification
		System.out.println("Identification valide : " + serv.identification("admin", "admin"));
		
		//Tests patient
		serv.addPatient(new Patient(0, "M", "Jean", "Pierre", "20-12-1964", 500000, "pierre@jean.fr", "Rien..."));
		
		ArrayList<Patient> ap = serv.listPatient();
		
		for (Patient patient : ap) {
			System.out.println(patient.toString());
		}
		
		System.out.println(serv.selectPatient(1).toString());
		
		//Tests medoc
		serv.addMedoc(new Medoc(0, "Pruneaux", "5 fruits par jours"));
		serv.addMedoc(new Medoc(0, "Fuqua", "Y a plus qu'a"));
		
		serv.modMedoc(3, new Medoc(0, "Mediatotor", "Toujours pas mort !"));
		
		
		ArrayList<Medoc> am = serv.listMedoc();
		for (Medoc medoc : am) {
			System.out.println(medoc.toString());
		}
		
		
		System.out.println(serv.selectMedoc(3).toString());
		
		//serv.dropMedoc(4);
		
		Ordonnance ordo = new Ordonnance(12345, serv.selectMedecin(2), serv.selectPatient(2), serv.listMedoc());
		CreatePDF pdf = new CreatePDF("./",ordo);
		pdf.generate();
		
		
		serv.close();
		
	} catch (RemoteException e) {
		e.printStackTrace();
	} catch (NotBoundException e) {
		e.printStackTrace();
		System.out.println("Connexion client \t\t\t\t\t\t\t\t\t\t[ECHEC]");
	} catch (FileNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (DocumentException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
		
	}

}
